package njci.softerware.com.myexpress2017.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import njci.softerware.com.myexpress2017.R;
import njci.softerware.com.myexpress2017.adapter.MyListAdapter;
import njci.softerware.com.myexpress2017.adapter.Package;
import njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher;
import njci.softerware.com.myexpress2017.serviceObj.PackageRecevie;
import njci.softerware.com.myexpress2017.serviceObj.PackageSign;
import njci.softerware.com.myexpress2017.serviceObj.dao.PackageDispatcherDao;
import njci.softerware.com.myexpress2017.serviceObj.dao.PackageRecevieDao;
import njci.softerware.com.myexpress2017.serviceObj.dao.PackageSignDao;
import njci.softerware.com.myexpress2017.serviceObj.dao.SharedPreferences;
import njci.softerware.com.myexpress2017.utils.WeekToDay;

public class PackageTableDetail extends AppCompatActivity {

    ListView packgeList = null;
    MyListAdapter adapter;
    List list;
    TextView textHeadTitle;
    String headTitle;
    SharedPreferences sh = null;
    Integer peopleId;
    int postDelayWeek;
    String date;
    List<String> weekList;
    @Bind(R.id.btnBack)
    Button btnBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_package_table_detail);
        ButterKnife.bind(this);
        btnBack.setVisibility(View.VISIBLE);
        sh = SharedPreferences.getInstance();
        peopleId = sh.getInt("userId", 0);
        packgeList = (ListView) findViewById(R.id.packgeList);
        textHeadTitle = (TextView) findViewById(R.id.textHeadTitle);
        int postType = getIntent().getExtras().getInt("activityType");
        date = getIntent().getExtras().getString("dateType");
        if (date == null) {
            postDelayWeek = getIntent().getExtras().getInt("postDelayWeek");
            weekList = WeekToDay.getWeekbeginAndEnd(postDelayWeek);
        }
        switch (postType) {
            case 0:
                list = findListRecevie(date, weekList);
                headTitle = "本周揽收";
                break;
            case 1:
                headTitle = "本周派送";
                list = findListDispatcher(date, weekList);
                break;
            case 2:
                headTitle = "本周签收";
                list = findListSignDate(date, weekList);
                break;
        }
        textHeadTitle.setText(headTitle);
        adapter = new MyListAdapter(this, list);
        packgeList.setAdapter(adapter);

    }


    private List findListSignDate(String date, List<String> weekList) {
        List<Package> packageList = new ArrayList<Package>();
        PackageSignDao packageSignDao = new PackageSignDao(getApplicationContext());
        List<PackageSign> packageSignList = packageSignDao.getListbySelection(peopleId, date, weekList);
        Iterator<PackageSign> it = packageSignList.iterator();
        while (it.hasNext()) {
            PackageSign pSign = it.next();
            Package p = new Package(pSign.getNAME(), pSign.getCount_sign(), pSign.getSign_date(), pSign.getUser_id());
            packageList.add(p);
        }
        return packageList;
    }

    private List findListDispatcher(String date, List<String> weekList) {
        List<Package> packageList = new ArrayList<Package>();
        PackageDispatcherDao packageDispatcherDao = new PackageDispatcherDao(getApplicationContext());
        List<PackageDispatcher> packageDispatcherList = packageDispatcherDao.getListCountBySelection(peopleId, date, weekList);
        Iterator<PackageDispatcher> it = packageDispatcherList.iterator();
        while (it.hasNext()) {
            PackageDispatcher packageDispatcher = it.next();
            Package p = new Package(packageDispatcher.getNAME(), packageDispatcher.getCount_dispatch(), packageDispatcher.getDispatcher_date(), packageDispatcher.getUser_id());
            packageList.add(p);
        }
        return packageList;
    }

    private List findListRecevie(String date, List<String> weekList) {
        List<Package> packageList = new ArrayList<Package>();
        PackageRecevieDao packageRecevieDao = new PackageRecevieDao(getApplicationContext());
        List<PackageRecevie> packageDispatcherList = packageRecevieDao.getListBySelection(peopleId, date, weekList);
        Iterator<PackageRecevie> it = packageDispatcherList.iterator();
        while (it.hasNext()) {
            PackageRecevie packageRecevie = it.next();
            Package p = new Package(packageRecevie.getNAME(), packageRecevie.getCount_recevie(), packageRecevie.getRe_date(), packageRecevie.getUser_id());
            packageList.add(p);
        }
        return packageList;
    }

    @Override
    protected void onDestroy() {
        date = null;
        weekList = null;
        super.onDestroy();
    }

    @OnClick(R.id.btnBack)
    public void onClick() {
        finish();
    }
}
